import {Component, OnInit, Input, OnDestroy} from '@angular/core';
import {Subscription} from 'rxjs';
import {AppGlobalService} from '../../shared/service/app-global.service';
import {MessageService} from '../../shared/service/message.service';
import {HttpService} from '../../shared/service/http.service';
import {ModalController} from '@ionic/angular';

@Component({
    selector: 'app-list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit, OnDestroy {

    // 组件数据
    @Input() tagData: any = {};

    @Input() ifModal?: boolean;

    // 声明订阅对象
    subscription: Subscription = null;

    // 筛选及页面数据参数
    params: any = {};

    // 当前页数，默认1
    pageNo = 1;

    // size
    pageSize: number;

    // 数量
    pageTotal: number;

    listData: any;

    defaultImg: String = '../../assets/img/default_img.png';

    constructor(
        private appGlobalService: AppGlobalService,
        private message: MessageService,
        private http: HttpService,
        private modalController: ModalController
    ) {
    }

    ngOnInit() {

        console.log(this.tagData);


        // 初始化参数
        this.params = {
            content: {},
            id: this.tagData.appListData.key,
            sessionId: localStorage.getItem('sessionId')
        };
        this.params.content = this.tagData.appListData.condition;
        console.log(this.params)

        this.listData = this.tagData.appListData.data;

        this.pageSize = this.tagData['pageSize'];
        this.pageTotal = this.tagData['pageTotal'];

        if (!this.subscription) {
            // 获取筛选参数
            this.subscription = this.message.getMessage().subscribe(param => {
                // 重置当前页数
                this.pageNo = 1;
                // 参数
                this.params['content'] = param;
                // 页面数据参数
                this.params['content']['pageNo'] = this.pageNo;
                // 请求获取新数据
                this.http.post('application/submit', this.params, res => {
                    // 请求数据成功
                    if (res['success']) {
                        const result = JSON.parse(res['data']['data']);
                        console.log(result);
                        // 筛选数据
                        this.listData = result.data;
                    } else {
                        // 请求数据失败
                        this.appGlobalService.showToast(res['message'], 2000, 'middle');
                    }
                });
            });
        }
    }

    ngOnDestroy(): void {
        // 组件生命周期结束的时候，注销
        this.subscription.unsubscribe();
    }


    /**
     * 列表跳转
     * @param {*} obj 跳转对象
     * @memberof StoreListComponent
     */
    clickLink(obj) {
        if (!obj) {
            console.log('没有返回link-target');
        }
        const pageParams = {
            pageId: obj.target,
            params: obj.params,
            sessionId: localStorage.getItem('sessionId')
        };
        this.appGlobalService.goToForward(pageParams);
        if (this.ifModal) {
            this.modalController.dismiss();
        }
    }


    /**
     * TODO: 下拉刷新，保留
     *
     * @param {*} event
     * @memberof StoreListComponent
     */
    doRefresh(event) {
    }

    /**
     * 上滑加载
     *
     * @param {*} event
     * @memberof StoreListComponent
     */
    loadData(event) {
        if (this.pageNo >= this.pageTotal) {
            event.target.disabled = true;
            // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
            // event.complete();
            return;
        }
        // 请求下一个页面
        this.pageNo = this.pageNo + 1;
        // 参数
        this.params['content']['pageNo'] = this.pageNo;
        // 请求获取新数据
        this.http.post('application/submit', this.params, res => {
            const result = JSON.parse(res.data);
            const resList = result.data;
            // 请求数据成功
            if (res['success']) {
                // 追加返回数据
                if (resList.data && resList.data.length > 0) {
                    // 追加数据
                    resList.data.map(item => {
                        this.listData['data'].push(item);
                    });
                } else {
                    // 没有数据了
                    // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
                }
                event.target.complete();
            } else {
                // 请求失败，更新当前页数
                this.pageNo = this.pageNo - 1;
                // 请求数据失败
                this.appGlobalService.showToast(res['message'], 2000, 'middle');
                event.target.complete();
            }
        });
    }
}
